﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Combine
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //选取文件夹，并获得路径

            FolderBrowserDialog f = new FolderBrowserDialog();
            if (f.ShowDialog() == DialogResult.OK)
            {
                String DirPath = f.SelectedPath;
                this.textBox1.Text = DirPath;//G:\新建文件夹
            }
            

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

// Directory.Move(this.textBox1.Text+this.textBox3.Text, this.textBox1.Text+this.textBox2.Text);
            copyDirectory(this.textBox1.Text + this.textBox3.Text, this.textBox1.Text + this.textBox2.Text);
            DialogResult result = MessageBox.Show("合并完成", "",
                          MessageBoxButtons.OK);

        }

        public static void copyDirectory(string sourceDirectory, string destDirectory)

        {

            //判断源目录和目标目录是否存在，如果不存在，则创建一个目录

            if (!Directory.Exists(sourceDirectory))

            {

                Directory.CreateDirectory(sourceDirectory);

            }

            if (!Directory.Exists(destDirectory))

            {

                Directory.CreateDirectory(destDirectory);

            }

            //拷贝文件

            copyFile(sourceDirectory, destDirectory);



            //拷贝子目录       

            //获取所有子目录名称

            string[] directionName = Directory.GetDirectories(sourceDirectory);



            foreach (string directionPath in directionName)

            {

                //根据每个子目录名称生成对应的目标子目录名称

                string directionPathTemp = destDirectory + "\\" + directionPath.Substring(sourceDirectory.Length + 1);



                //递归下去

                copyDirectory(directionPath, directionPathTemp);

            }

            DeleteDir(sourceDirectory);

        }

        public static void copyFile(string sourceDirectory, string destDirectory)

        {

            //获取所有文件名称

            string[] fileName = Directory.GetFiles(sourceDirectory);



            foreach (string filePath in fileName)

            {

                //根据每个文件名称生成对应的目标文件名称

                string filePathTemp = destDirectory + "\\" + filePath.Substring(sourceDirectory.Length + 1);



                //若不存在，直接复制文件；若存在，覆盖复制

                if (File.Exists(filePathTemp))

                {

                    File.Copy(filePath, filePathTemp, true);

                }

                else

                {

                    File.Copy(filePath, filePathTemp);

                }

            }

        }
        public static bool DeleteDir(string file)
        {
            try
            {
                //去除文件夹和子文件的只读属性
                //去除文件夹的只读属性
                System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
                fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
                //去除文件的只读属性
                System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
                //判断文件夹是否还存在
                if (Directory.Exists(file))
                {
                    foreach (string f in Directory.GetFileSystemEntries(file))
                    {
                        if (File.Exists(f))
                        {
                            //如果有子文件删除文件
                            File.Delete(f);
                            Console.WriteLine(f);
                        }
                        else
                        {
                            //循环递归删除子文件夹
                            DeleteDir(f);
                        }
                    }
                    //删除空文件夹
                    Directory.Delete(file);
                }
                return true;
            }
            catch (Exception ex) // 异常处理
            {
                return false;
            }

        }

    }

}

